package com.cennavi.minenavidemo.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.DisplayCutout;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.RequiresApi;

import com.cennavi.minenavidemo.R;
import com.gyf.barlibrary.BarHide;
import com.gyf.barlibrary.ImmersionBar;

import java.lang.reflect.Method;
import java.util.List;

import static android.content.Context.WINDOW_SERVICE;

/**
 * 获取一些屏幕相关的信息
 */
public class ScreenUtil {

    private static final String PHONE_FLAG_XIAO_MI = "xiaomi";
    private static final String PHONE_FLAG_HUA_WEI = "HUAWEI";
    private static final String PHONE_FLAG_OPPO = "oppo";
    private static final String PHONE_FLAG_VIVO = "vivo";

    public static final int NAVIGATION_BAR_ON_RIGHT = 1;
    public static final int NAVIGATION_BAR_ON_LiFT = 3;

    private Context mContext;
    private Window mWindow;
    private WindowManager mWindowManager;
    private View statusView;
    private int mNavigationBarHeight;
    private boolean isUserChangeSystemBrightness = false;
    private int mCurrentSystemBrightness = 100;
    private DisplayMetrics mDisplayMetrics;
    private int mNotchWidth = 0;
    private int mNotchHeight = 0;
    private int mHorizontalMaxWidth;

    public void init(Context context, Window window) {
        mContext = context;
        mWindow = window;
        mWindowManager = (WindowManager) mContext.getSystemService(WINDOW_SERVICE);
        mHorizontalMaxWidth = Utils.getInstance().dp2Px(465);
    }

    private static class SingletonHolder {
        public static final ScreenUtil instance = new ScreenUtil();
    }

    public static ScreenUtil getInstance() {
        return SingletonHolder.instance;
    }

    public int getNotchWidth() {
        if (mNotchWidth != 0) {
            return mNotchWidth;
        }

        int notchWidth = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            //小米8升级到MIUI 11后获取的刘海宽度有问题 为676 官方为560，有问题会影响自定义状态栏,为防止其他型号手机升级有问题，以下数据写死
            //来源 https://dev.mi.com/console/doc/detail?pId=1341
            if (Build.MODEL.equals("MI 8") || Build.MODEL.equals("MI 8 Explorer Edition") || Build.MODEL.equals("MI 8 UD")) {
                notchWidth = 560;
            } else if (Build.MODEL.equals("MI 8 SE")) {
                notchWidth = 540;
            } else if (Build.MODEL.equals("MI8Lite")) {
                notchWidth = 296;
            } else if (Build.MODEL.equals("POCO F1")) {
                notchWidth = 588;
            } else if (Build.MODEL.equals("Redmi 6 Pro")) {
                notchWidth = 352;
            } else if (Build.MODEL.equals("Redmi Note 7")) {
                notchWidth = 116;
            }

            if (notchWidth != 0) {
                return notchWidth;
            }

            DisplayCutout displayCutout = getDisplayCutout((Activity) mContext);
            if (displayCutout != null) {
                List<Rect> boundingRects = displayCutout.getBoundingRects();
                if (boundingRects.size() != 0) {
                    int left = boundingRects.get(0).left;
                    int screenWidth = getScreenWidth();
                    if (left > screenWidth / 2) {//刘海描述右边的区域
                        notchWidth = screenWidth - (screenWidth - left) * 2;
                    } else {
                        notchWidth = screenWidth - left * 2;
                    }
                }
            }
        } else {
            if (isXiaoMiPhone()) {
                notchWidth = getXiaoMiNotchSize(mContext)[0];
            } else if (isHUAWEIPhone()) {
                notchWidth = getHuaWeiNotchSize(mContext)[0];
            } else if (isOPPOPhone()) {
                notchWidth = 324;
            } else if (isVivoPhone()) {
                notchWidth = Utils.getInstance().dp2Px(100);
            }
        }
        mNotchWidth = notchWidth;
        return notchWidth;
    }

    public int getNotchHeight() {
        if (mNotchHeight != 0) {
            return mNotchHeight;
        }
        int notchHeight = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            DisplayCutout displayCutout = getDisplayCutout((Activity) mContext);
            if (displayCutout != null) {    //通过cutout是否为null判断目前是否开启了刘海屏
                List<Rect> boundingRects = displayCutout.getBoundingRects();
                if (boundingRects.size() != 0) {
                    Rect rect = boundingRects.get(0);
                    notchHeight = rect.height();
                    //如果刘海的高度大于150，证明是横屏，有问题，取width()
                    if (notchHeight > 150) {
                        notchHeight = rect.width();
                    }
                }
            } else {
                return notchHeight;
            }
        } else {
            if (isXiaoMiPhone()) {
                notchHeight = getXiaoMiNotchSize(mContext)[1];
            } else if (isHUAWEIPhone()) {
                notchHeight = getHuaWeiNotchSize(mContext)[1];
            } else if (isOPPOPhone()) {
                notchHeight = 80;
            } else if (isVivoPhone()) {
                notchHeight = Utils.getInstance().dp2Px(32);// 32dp为状态栏高度,刘海高度为27dp
            }
        }
        if (notchHeight == 0) {
            notchHeight = getStatusBarHeight();
        }
        mNotchHeight = notchHeight;
        return notchHeight;
    }

    public int getLiuHaiping() {
        if (mNotchHeight != 0) {
            return mNotchHeight;
        }
        int notchHeight = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            DisplayCutout displayCutout = getDisplayCutout((Activity) mContext);
            if (displayCutout != null) {    //通过cutout是否为null判断目前是否开启了刘海屏
                List<Rect> boundingRects = displayCutout.getBoundingRects();
                if (boundingRects.size() != 0) {
                    Rect rect = boundingRects.get(0);
                    notchHeight = rect.height();
                    //如果刘海的高度大于150，证明是横屏，有问题，取width()
                    if (notchHeight > 150) {
                        notchHeight = rect.width();
                    }
                }
            } else {
                return notchHeight;
            }
        } else {
            if (isXiaoMiPhone()) {
                notchHeight = getXiaoMiNotchSize(mContext)[1];
            } else if (isHUAWEIPhone()) {
                notchHeight = getHuaWeiNotchSize(mContext)[1];
            } else if (isOPPOPhone()) {
                notchHeight = 80;
            } else if (isVivoPhone()) {
                notchHeight = Utils.getInstance().dp2Px(32);// 32dp为状态栏高度,刘海高度为27dp
            }
        }
        return notchHeight;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private DisplayCutout getDisplayCutout(Activity activity) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            View decorView = activity.getWindow().getDecorView();
            if (decorView != null) {
                WindowInsets windowInsets = decorView.getRootWindowInsets();
                if (windowInsets != null) {
                    return windowInsets.getDisplayCutout();
                }
            }
        }
        return null;
    }

    /**
     * 获取状态栏高度
     */
    public int getStatusBarHeight() {
        int statusBarHeight = 0;

        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusBarHeight = mContext.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (statusBarHeight == 0) {
            int resourceId = mContext.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                statusBarHeight = mContext.getResources().getDimensionPixelSize(resourceId);
            }
        }

        if (statusBarHeight == 0) {
            Rect rectangle = new Rect();
            mWindow.getDecorView().getWindowVisibleDisplayFrame(rectangle);
            statusBarHeight = rectangle.top;
        }

        return statusBarHeight;
    }

    /**
     * 能否用自定义状态栏
     */
    public boolean useCustomStatusBar() {
        if (!canUseImmersive()) {
            return false;
        }

        if (isXiaoMiOpenHideFullScreenToggle(mContext)) {
            return false;
        }
        return true;
    }

    /**
     * 获取小米手机刘海屏宽高  int[0] 为宽度   int[1] 为高度
     */
    private int[] getXiaoMiNotchSize(Context context) {
        int[] ret = new int[]{0, 0};
        int resourceWidthId = context.getResources().getIdentifier("notch_width", "dimen", "android");
        if (resourceWidthId > 0) {
            ret[0] = (context.getResources().getDimensionPixelSize(resourceWidthId)) + 10;
        }
        int resourceHeightId = context.getResources().getIdentifier("notch_height", "dimen", "android");
        if (resourceHeightId > 0) {
            ret[1] = context.getResources().getDimensionPixelSize(resourceHeightId) + 30;
        }
        return ret;
    }

    /**
     * 获取华为手机刘海屏宽高  int[0] 为宽度   int[1] 为高度
     */
    private int[] getHuaWeiNotchSize(Context context) {
        int[] ret = new int[]{0, 0};
        try {
            ClassLoader cl = context.getClassLoader();
            Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = HwNotchSizeUtil.getMethod("getNotchSize");
            ret = (int[]) get.invoke(HwNotchSizeUtil);
        } catch (ClassNotFoundException e) {
            Log.e("test", "getNotchSize ClassNotFoundException");
        } catch (NoSuchMethodException e) {
            Log.e("test", "getNotchSize NoSuchMethodException");
        } catch (Exception e) {
            Log.e("test", "getNotchSize Exception");
        } finally {
            return ret;
        }
    }

    public boolean isXiaoMiPhone() {
        return Build.MANUFACTURER.equalsIgnoreCase(PHONE_FLAG_XIAO_MI);
    }

    public boolean isHUAWEIPhone() {
        return Build.MANUFACTURER.equalsIgnoreCase(PHONE_FLAG_HUA_WEI);
    }

    public boolean isOPPOPhone() {
        return Build.MANUFACTURER.equalsIgnoreCase(PHONE_FLAG_OPPO);
    }

    public boolean isVivoPhone() {
        return Build.MANUFACTURER.equalsIgnoreCase(PHONE_FLAG_VIVO);
    }

    /**
     * 小米是否开启了隐藏全面屏的开关(系统设置里)
     */
    public boolean isXiaoMiOpenHideFullScreenToggle(Context context) {
        return ImmersionBar.hasNotchScreen((Activity) context) && isXiaoMiPhone()
                && Settings.Global.getInt(context.getContentResolver(), "force_black", 0) == 1;
    }

    /**
     * 获取当前屏幕的亮度
     *
     * @param context
     * @return 返回是一个int值 在0 ~ 255之间
     */
    public int getSystemBrightness(Context context) {
        int nowBrightnessValue = 0;
        try {
            nowBrightnessValue = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nowBrightnessValue;
    }

    /**
     * 设置当前系统屏幕亮度值  0 ~ 255
     *
     * @param context
     * @param paramInt 设置的值
     */
    public void setSystemScreenBrightness(Context context, int paramInt) {
        try {
            Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, paramInt);
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    /**
     * 判断是否开启了自动亮度调节
     * 自动 = 1 SCREEN_BRIGHTNESS_MODE_AUTOMATIC
     * 手动 = 0 SCREEN_BRIGHTNESS_MODE_MANUAL
     *
     * @param context
     * @return
     */
    public boolean isAutoBrightness(Context context) {
        boolean isAutoBrightness = false;
        try {
            isAutoBrightness = Settings.System.getInt(
                    context.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return isAutoBrightness;
    }

    /**
     * 改变App当前Window亮度
     * lp.screenBrightness 范围 0.0 ~ 1.0之间
     *
     * @param brightness
     * @param context    activity的context
     */
    public void changeAppBrightness(Context context, float brightness) {
        Window window = ((Activity) context).getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        if (brightness == -1) {
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
        } else {
            lp.screenBrightness = brightness;
        }
        window.setAttributes(lp);
    }

    /**
     * 停止自动亮度调节
     *
     * @param context
     */
    public void stopAutoBrightness(Context context) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }

    /**
     * 开启亮度自动调节
     *
     * @param context
     */
    public void startAutoBrightness(Context context) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    }

    public void cleanup() {
        mContext.getContentResolver().unregisterContentObserver(mBrightnessObserver);
    }

    /**
     * 设置降低屏幕亮度策略
     *
     * @param enable 是否打开
     */
    public void enableScreenBrightnessDarkStrategy(boolean enable) {
        if (Utils.getInstance().isCharging() || isAutoBrightness(mContext)) {
            return;
        }
        if (enable) {
            isUserChangeSystemBrightness = false;
            mCurrentSystemBrightness = getSystemBrightness(mContext);
            setSystemScreenBrightness(mContext, mCurrentSystemBrightness / 2);
            mContext.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS), false, mBrightnessObserver);
        } else {
            mContext.getContentResolver().unregisterContentObserver(mBrightnessObserver);
            if (!isUserChangeSystemBrightness)
                setSystemScreenBrightness(mContext, mCurrentSystemBrightness);
        }
    }

    /**
     * 注册光观察者: context.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS), false, mBrightnessObserver);
     * 应用销毁解观察者: context.getContentResolver().unregisterContentObserver(mBrightnessObserver);
     * 如果开启自动亮度调节,获取系统亮度,是一个值,调节bar,获取系统亮度还是那个值
     * 如果关闭自动亮度调节,获取的亮度是一个值,调节bar,获取系统亮度是调节后的值
     * 如果开启自动亮度调节,此方法监听不到系统亮度变化
     */
    private ContentObserver mBrightnessObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            super.onChange(selfChange, uri);
            isUserChangeSystemBrightness = true;
        }
    };

    public Window getWindow() {
        return mWindow;
    }

    /**
     * 获取屏幕宽 可用显示区域大小
     */
    public int getScreenWidth() {
        mDisplayMetrics = new DisplayMetrics();
        mWindowManager.getDefaultDisplay().getMetrics(mDisplayMetrics);
        return mDisplayMetrics.widthPixels;
    }

    public int getScreenHeight() {
        mDisplayMetrics = new DisplayMetrics();
        mWindowManager.getDefaultDisplay().getMetrics(mDisplayMetrics);
        return mDisplayMetrics.heightPixels;
    }

    /**
     * 获取横向屏幕可用显示区域最大宽度
     */
    public int getHorizontalMaxWidth() {
        int screenWidthTwoFifths = getScreenWidth() * 2 / 5;
        if (screenWidthTwoFifths > mHorizontalMaxWidth) {
            return mHorizontalMaxWidth;
        } else {
            return screenWidthTwoFifths;
        }
    }

    /**
     * 地图显示的高,获取的是正常竖屏的高
     */
    public int getMapShowHeight() {
        int mapShowHeight;
        if (canUseImmersive()) {
            mapShowHeight = getRealScreenHeight() - getNavigationBarHeight();
        } else {
            mapShowHeight = getRealScreenHeight() - getNavigationBarHeight() - getStatusBarHeight();
        }
        return mapShowHeight;
    }

    /**
     * 通过反射拿到屏幕实际的高度(包括状态栏和导航栏),获取的是正常竖屏的高
     */
    private int getRealScreenHeight() {
        int dpi = 0;
        Display display = ((Activity) mContext).getWindowManager().getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        @SuppressWarnings("rawtypes")
        Class c;
        try {
            c = Class.forName("android.view.Display");
            @SuppressWarnings("unchecked")
            Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
            method.invoke(display, dm);
            if (isScreenLandscape()) {
                dpi = dm.widthPixels;
            } else {
                dpi = dm.heightPixels;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (dpi == 0) {
            if (isScreenLandscape()) {
                dpi = getScreenWidth();
            } else {
                if (ImmersionBar.hasNotchScreen((Activity) mContext)) {
                    dpi = getAvailableScreenHeight() + getNotchHeight() + getNavigationBarHeight();
                } else {
                    dpi = getAvailableScreenHeight() + getStatusBarHeight() + getNavigationBarHeight();
                }
            }
        }
        return dpi;
    }

    /**
     * 计算屏幕可用高度(此方法会受到软键盘 状态栏 底部导航栏的影响，不适合view自绘中调用，性能会有损耗)
     * rect的top 是指 如果状态栏显示 则top值是状态栏高度 如果不显示则为0
     * rect的bottom 是指 如果底部导航栏显示 bottom是屏幕的高度减去导航栏的高度  如果底部导航栏不显示 bottom的值是屏幕高度
     */
    private int horizontalAvailableScreenHeight, verticalAvailableScreenHeight;

    private int getAvailableScreenHeight() {
        if (isScreenLandscape()) {
            if (horizontalAvailableScreenHeight == 0) {
                Rect rect = new Rect();
                mWindow.getDecorView().getWindowVisibleDisplayFrame(rect);
                horizontalAvailableScreenHeight = rect.right - rect.left;
            }
            return horizontalAvailableScreenHeight;
        } else {
            if (verticalAvailableScreenHeight == 0) {
                Rect rect = new Rect();
                mWindow.getDecorView().getWindowVisibleDisplayFrame(rect);
                verticalAvailableScreenHeight = rect.bottom - rect.top;
            }
            return verticalAvailableScreenHeight;
        }
    }

    /**
     * 设置是否全屏
     */
    public void enableFullScreen(boolean enable) {
        if (ImmersionBar.hasNotchScreen((Activity) mContext)) {
            setStatusBarVisible(!enable);
        } else {
            if (enable) {
                ImmersionBar.with((Activity) mContext)
                        .hideBar(BarHide.FLAG_HIDE_STATUS_BAR)
                        .init();
            } else {
                ImmersionBar.with((Activity) mContext)
                        .hideBar(BarHide.FLAG_SHOW_BAR)
                        .init();
            }
        }
    }

    /**
     * 设置状态栏是否可见
     */
    private void setStatusBarVisible(boolean show) {
        int uiFlags;
        if (show) {
            uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            uiFlags |= 0x00001000;
        } else {
            uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            uiFlags |= 0x00001000;
        }
        mWindow.getDecorView().setSystemUiVisibility(uiFlags);
    }

    /**
     * 获取导航栏虚拟按键的高度，如果虚拟按键隐藏就返回0，否则返回虚拟按键的高度
     */
    public int getNavigationBarHeight() {
        if (isNavigationBarShow()) {
            return calculateNavigationBarHeight();
        } else {
            mNavigationBarHeight = 0;
            return mNavigationBarHeight;
        }
    }

    /**
     * 获取忽略导航栏隐藏来获取导航栏的高，即算导航栏显示时候的高
     */
    public int getNavigationBarHeightIgnoreHide() {
        if (mNavigationBarHeight != 0) {
            return mNavigationBarHeight;
        } else {
            return calculateNavigationBarHeight();
        }
    }

    /**
     * 计算虚拟按键的高度
     */
    private int calculateNavigationBarHeight() {
        if (mNavigationBarHeight == 0) {
            try {
                Class<?> clazz = Class.forName("com.android.internal.R$dimen");
                Object object = clazz.newInstance();
                int height = Integer.parseInt(clazz.getField("navigation_bar_height").get(object).toString());
                mNavigationBarHeight = mContext.getResources().getDimensionPixelSize(height);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (mNavigationBarHeight == 0) {
            Resources res = mContext.getResources();
            int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                mNavigationBarHeight = res.getDimensionPixelSize(resourceId);
            }
        }

        if (mNavigationBarHeight == 0) {
            Rect rect = new Rect();
            mWindow.getDecorView().getWindowVisibleDisplayFrame(rect);
            Point realSize = new Point();
            mWindowManager.getDefaultDisplay().getRealSize(realSize);
            mNavigationBarHeight = realSize.y - rect.bottom;
        }
        return mNavigationBarHeight;
    }

    /**
     * 是否保持屏幕常亮
     *
     * @param enable true为保持常亮
     */
    public void enableKeepScreenOn(boolean enable) {
        if (enable) {
            mWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        } else {
            mWindow.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }

    public void setNotchShowContent(boolean isShow) {
        setNotchShowContent(mWindow, isShow);
    }

    /**
     * 设置刘海区是否显示内容
     *
     * @param window 当前显示的window
     * @param isShow 刘海区域是否显示内容，如果false即刘海区域变黑，不显示内容
     */
    public void setNotchShowContent(Window window, boolean isShow) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
            if (isShow) {
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
                window.setAttributes(lp);
            } else {
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER;
                window.setAttributes(lp);
            }
        }
    }

    /**
     * 判断虚拟导航栏是否显示
     *
     * @return true(显示虚拟导航栏)，false(不显示或不支持虚拟导航栏)
     */
    private boolean isNavigationBarShow() {
        //判断小米手机是否开启了全面屏,开启了，直接返回false
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (Settings.Global.getInt(mContext.getContentResolver(), "force_fsg_nav_bar", 0) != 0) {
                return false;
            }
        }
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1 || isHUAWEIPhone()) {
            int screenRealHeight = getRealScreenHeight();
            int statusBarHeight = getStatusBarHeight();
            int screenAvailableHeight = getAvailableScreenHeight();

            return screenAvailableHeight + statusBarHeight < screenRealHeight;
        } else {
            //其他手机根据屏幕真实高度与显示高度是否相同来判断
            Display d = mWindowManager.getDefaultDisplay();

            DisplayMetrics realDisplayMetrics = new DisplayMetrics();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                d.getRealMetrics(realDisplayMetrics);
            }

            int realHeight = realDisplayMetrics.heightPixels;
            int realWidth = realDisplayMetrics.widthPixels;

            DisplayMetrics displayMetrics = new DisplayMetrics();
            d.getMetrics(displayMetrics);

            int displayHeight = displayMetrics.heightPixels;
            int displayWidth = displayMetrics.widthPixels;

            return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
        }
    }

    /**
     * 判断手机是否使用了沉浸式
     *
     * @return true 表示使用了 false表示没使用
     */
    public boolean canUseImmersive() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
    }

    /**
     * 判断当前屏幕方向是否为横屏
     *
     * @param context 当前需要判断是否是横屏的Activity的Context
     * @return true 横屏  false 竖屏
     */
    public boolean isScreenLandscape(Context context) {
        Configuration mConfiguration = context.getResources().getConfiguration(); //获取设置的配置信息
        int ori = mConfiguration.orientation; //获取屏幕方向
        return ori == Configuration.ORIENTATION_LANDSCAPE;
    }

    /**
     * 判断当前屏幕横屏方向
     */
    public int horizontalDirection() {
        return ((Activity) mContext).getWindowManager().getDefaultDisplay().getRotation();
    }

    /**
     * 判断当前屏幕方向是否为横屏,注意 此方法适用于关于MainActivity的横竖屏判断
     *
     * @return true 横屏  false 竖屏
     */
    public boolean isScreenLandscape() {
        return isScreenLandscape(mContext);
    }

    public int getDimen10Px() {
        return mContext.getResources().getDimensionPixelOffset(R.dimen.nz_px_10);
    }

    public int getDimen20Px() {
        return mContext.getResources().getDimensionPixelOffset(R.dimen.nz_px_20);
    }

    //主页面底部抽拉菜单适配刘海屏
    public int getAdaptationLiuHaipingHeigh() {
        if (getLiuHaiping() == 0) {
            return mContext.getResources().getDimensionPixelSize(R.dimen.nz_px_56);
        }
        return getLiuHaiping();
    }

    private Drawable mShadowDrawable;
    private Rect mDrawablePadding;


    public Rect getShadowBgRect(int drawableResId) {
        mShadowDrawable = mContext.getResources().getDrawable(drawableResId);
        mDrawablePadding = new Rect();
        mShadowDrawable.getPadding(mDrawablePadding);
        return mDrawablePadding;
    }
}